import { observable, action } from 'mobx';
import { WeaForm, WeaTableNew } from 'comsMobx';
import { WeaLocaleProvider } from 'ecCom';
import { message } from 'antd';
import CommonStore from './commonStore';
import * as SEND_DOC_APIS from '../apis/sendDoc';
import * as odocUtil from '../util/odocUtil';

const getLabel = WeaLocaleProvider.getLabel;
const { TableStore } = WeaTableNew;

export default class SendDoc extends CommonStore {
  test = '发送公文';
  @observable status = {
    condition: '',
    settedUnit: '', // 流程中选择的收发文单位信息
    otherUnit: '', // 其他收发文单位信息
  }
  @observable odocSendDocDialogForm = new WeaForm();
  @observable selectRowId = [];
  @observable FTPShowRecive = false;

  /* 收文单位信息弹窗 */
  @observable showRecive = false; // 收文单位信息弹窗显隐
  @observable reciveDocUnitTable = new TableStore();// 收文单位信息列表
  @observable documentIdentifier = '';// 公文标识
  @observable dialogType = 'invalid';// 弹窗类型 撤销还是作废 invalid backout
  @observable showReasonDialog = false;// 是否显示拒收或撤销理由弹窗
  @observable backoutForm = new WeaForm();// 撤销 form
  @observable invalidForm = new WeaForm();// 作废 form
  @observable unitIdArr = [];// 收文单位id
  @observable requestId = [];// 请求id 重新发送时使用
  @observable isInnerChange = '0'

  constructor() {
    super();
    this.pageName = 'sendDoc';
  }

  getReceiveSendUnitInfo = (params, callback) => {
    SEND_DOC_APIS.getReceiveSendUnitInfo(params).then((result) => {
      this.status = {
        ...this.status,
        condition: result.condition,
        settedUnit: result.settedUnit,
        otherUnit: result.otherUnit,
      };
      if (callback) callback(result.condition);
    });
  }

  setReceiveSendUnitInfo = (params, callback) => {
    SEND_DOC_APIS.setReceiveSendUnitInfo(params).then((result) => {
      if (result.status) {
        this.status = {
          ...this.status,
          // condition: result.condition,
          settedUnit: result.settedUnit,
          otherUnit: result.otherUnit,
        };
        if (callback) callback();
        message.success(getLabel(27564, '发送成功'));
        this.onlyClose();
        this.initData({
          compParams: {
            viewcondition: this.viewcondition,
            viewconditiondomkey: 'state',
            isInnerChange: this.isInnerChange,
            state: this.viewcondition,
          },
        });
      } else {
        message.error(getLabel(22397, '发送失败'));
      }
    });
  }

  // 批量选择公文
  @action.bound selelctRow (selectRowId, selectRows) {
    this.selectRowId = selectRowId;
    this.requestId = [];
    for (let row of selectRows) {
      this.requestId.push(row.requestid);
    }
  }

  // 收文单位信息选择行
  @action.bound onUnitRowSelect (selectRowId, selectedRows) {
    this.unitIdArr = [];
    for (let row of selectedRows) {
      this.unitIdArr.push(row.receive_company_id);
    }
  }

  // 关闭收文单位信息弹窗
  @action.bound closeReciveDialog () {
    this.documentIdentifier = '';
    this.showRecive = false;
    this.unitIdArr = [];
    this.selectRowId = [];
  }

  // 获取查看收文单位信息的table数据
  @action.bound getSendDocinfo (entityName = '') {
    const params = {
      documentId: this.documentIdentifier, // 公文标识
      entityName, // 搜索参数  单位名称
    };
    SEND_DOC_APIS.getSendDocinfo(params).then((data) => {
      if (data.sessionkey) {
        this.reciveDocUnitTable.getDatas(data.sessionkey, 1, 10);
      }
    });
  }

  // 撤销
  @action.bound backout (record = {}) {
    this.dialogType = 'backout';
    this.rerender = Math.random();
    this.showReasonDialog = true;
    if (record.id) {
      this.unitIdArr = [];
      this.unitIdArr.push(record.receive_company_id);
    }
  }

  // 理由弹窗的关闭
  @action.bound hideReasonDialog () {
    this.showReasonDialog = false;
    if (this.dialogType == 'invalid') {
      this.requestId = [];
      this.selectRowId = [];
    }
    this.invalidForm.resetForm();
    this.backoutForm.resetForm();
  }

  // 获取撤销、作废的form
  @action.bound postWebSendDocCondition () {
    SEND_DOC_APIS.postWebSendDocCondition().then((data) => {
      this.invalidForm.setCondition(data.invalidCondition);
      this.backoutForm.setCondition(data.undoCondition);
    });
  }

  // 撤销提交
  @action.bound backoutSubmit () {
    const formData = this.backoutForm.getFormParams();
    const ids = this.selectRowId.join(',');
    const receiveUnitIds = this.unitIdArr.join(',');
    const params = {
      ids, // 每条收文数据的id
      detail: formData.detail, // 处理意见
      status: '4', // 撤销的状态
      receiverDepartments: receiveUnitIds, // 单位id
      isInnerChange: this.isInnerChange,
    };
    SEND_DOC_APIS.revokedDoc(params).then((data) => {
      if (data.api_status) {
        this.hideReasonDialog();
        this.closeReciveDialog();
        this.doSearch({
          ifAdFormCgWhenCgTab: true,
          behavior: 'changeTab',
          viewcondition: this.viewcondition,
          viewconditiondomkey: 'state',
          otherInterfaceParams: {
            isInnerChange: this.isInnerChange,
            state: this.viewcondition,
          },
        });
        message.success(getLabel('30700', '操作成功'));
      } else {
        message.error(data.api_message);
      }
    });
  }

  // 作废
  @action.bound invalidSubmit () {
    const formData = this.invalidForm.getFormParams();
    const ids = this.selectRowId.join(',');
    const params = {
      ids,
      detail: formData.detail,
      status: '5',
      isInnerChange: this.isInnerChange,
    };
    SEND_DOC_APIS.cancelledDoc(params).then((data) => {
      if (data.api_status) {
        this.doSearch({
          ifAdFormCgWhenCgTab: true,
          behavior: 'changeTab',
          viewcondition: this.viewcondition,
          viewconditiondomkey: 'state',
          otherInterfaceParams: {
            isInnerChange: this.isInnerChange,
            state: this.viewcondition,
          },
        });
        this.hideReasonDialog();
        message.success(getLabel('30700', '操作成功'));
      } else {
        message.error(data.api_message);
      }
    });
  }

  // 重新发送
  @action.bound resendSubmit (unitIds) {
    if (unitIds == '') {
      return;
    }
    this.loading = true;
    const ids = this.requestId.join(',');
    const params = {
      ids,
      status: '0',
      receiverDepartments: unitIds,
      isInnerChange: this.isInnerChange,
    };
    SEND_DOC_APIS.resendDoc(params).then((data) => {
      this.loading = false;
      if (data.api_status) {
        this.doSearch({
          ifAdFormCgWhenCgTab: true,
          behavior: 'changeTab',
          viewcondition: this.viewcondition,
          viewconditiondomkey: 'state',
          otherInterfaceParams: {
            isInnerChange: this.isInnerChange,
            state: this.viewcondition,
          },
        });
        message.success(getLabel('30700', '操作成功'));
        this.selectRowId = [];
        this.requestId = [];
        this.showRecive = false;
        this.documentIdentifier = '';
      } else {
        message.error(data.api_message);
      }
    });
  }

  /* ---------------FTP模式-----------------------*/
  // ftp收文信息关闭
  @action.bound
  onlyClose () {
    this.requestId = '';
    this.FTPShowRecive = false;
  }

  // FTP模式下 flag为 0  查看收文 1 作废 2 重新发送 3 发送
  // webService模式下flag 为  21查看收文详情 22作废 23重新发送 24发送
  @action.bound
  onOperatesClick = (record, index, operate, flag, component) => {
    this.selectRowId = [];
    this.requestId = [];
    if (flag == '0') {
      this.requestId = record.requestid;
      this.FTPShowRecive = true;
      this.getReceiveSendUnitInfo({ requestid: this.requestId },
        (condition) => {
          this.odocSendDocDialogForm.initFormFields(condition);
        },
      );
    } else if (flag == '1') {

    } else if (flag == '2') {

    } else if (flag == '3') {
      this.selectRowId.push(record.requestid);
      this.onFTPSend();
    } else if (flag == '21') {
      this.documentIdentifier = record.document_identifier;
      this.requestId.push(record.requestid);
      this.selectRowId.push(record.id);
      this.getSendDocinfo();
      this.showRecive = true;
    } else if (flag == '22') {
      this.selectRowId.push(record.id);
      this.dialogType = 'invalid';
      this.showReasonDialog = true;
    } else if (flag == '23') {
      this.requestId.push(record.requestid);
      component.refs.browser.openModal();
    } else if (flag == '24') {
      if (this.loading) {
        return;
      }
      this.selectRowId.push(record.id);
      odocUtil.onsendDocBtnClick(this);
    }
  };

  @action.bound
  setMobxState (params = {}) {
    Object.keys(params).forEach((key) => {
      this[key] = params[key];
    });
  }

  // FTP 批量发送
  @action.bound
  onFTPSend () {
    this.loading = true;
    const params = {
      ids: this.selectRowId.join(','),
      status: 1,
      isInnerChange: this.isInnerChange,
    };
    SEND_DOC_APIS.sendDoc(params).then((data) => {
      this.getList(data);
    });
  }

  // 获取数据
  @action.bound
  getList (data) {
    this.sloading = false;
    if (data.api_status) {
      if (data.api_message == '') {
        message.success(getLabel('30700', '操作成功'));
      } else {
        message.success(data.api_message);
      }
    } else {
      message.error(data.api_message);
    }
    this.onSearch({ behavior: 'search' });
    this.selectRowId = [];
  }

  @action.bound
  onSearch ({ behavior, viewcondition }) {
    this.selectRowId = [];
    this.setStoreAttr('ifShowSearchAd', false);
    this.doSearch({
      ifAdFormCgWhenCgTab: true,
      behavior,
      viewcondition: viewcondition || this.viewcondition,
      viewconditiondomkey: 'state',
      otherInterfaceParams: {
        isInnerChange: this.isInnerChange,
        state: viewcondition || this.viewcondition,
      },
    });
  }

  // 显示列定制
  @action.bound
  customSet () {
    this.tableStore.setColSetVisible(true);
    this.tableStore.tableColSet(true);
  }
}
